﻿using LineOfBusiness.Controls.Base;
using LineOfBusiness.DataAccess;
using LineOfBusiness.Poco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace LineOfBusiness.BS.Controllers
{
    public class DepartmentsController : LOBController
    {
        public DepartmentsController()
        {
            this.GridViewName = "grvtblDepartments";
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            ViewBag.Edit = 0;
            CF_tblDepartments Departments = new CF_tblDepartments();
            return View("Input", Departments);
        }

        public ActionResult View(string DepID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                CF_tblDepartments Departments = dal.Repository.GetQuery<CF_tblDepartments>().Where(o => o.DepID == DepID).FirstOrDefault();
                ViewBag.Edit = -1;
                return View("Input", Departments);
            }
        }

        public ActionResult Edit(string DepID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                CF_tblDepartments Departments = dal.Repository.GetQuery<CF_tblDepartments>().Where(o => o.DepID == DepID).FirstOrDefault();
                if (this.Request.QueryString.AllKeys.Contains("View"))
                    ViewBag.Edit = -1;
                else
                    ViewBag.Edit = 1;

                return View("Input", Departments);
            }

        }

        [HttpPost]
        public ActionResult Delete(CF_tblDepartments Departments)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    dal.Repository.Delete(Departments);
                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

        [HttpPost]
        public ActionResult Save(CF_tblDepartments Departments)
        {
            string method = "Edit";
            string KeyOld = "";

            if (ViewData["Edit"].Equals("0"))
            {
                method = "Add";
                Departments.CreatedDate = DateTime.Now;
                Departments.UserID = LoginUser.UserID;

            }
            else
            {
                KeyOld = ViewData["ID"] as string;
            }

            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    if (method == "Add")
                    {
                        dal.Repository.Add(Departments);
                    }
                    else
                    {
                        if (KeyOld == Departments.DepID)
                        {
                            dal.Repository.Update(Departments);
                        }
                        else
                        {
                            dal.Repository.Delete<CF_tblDepartments>(o => o.DepID == KeyOld);
                            dal.Repository.Add(Departments);
                        }
                    }

                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

    }
}
